
import "../css/update.css"
import $ from './jquery';
import './401';

$(function () {

    //图片点击
    $("#img").click(function () {
        $("#file").click();
    })

    let filename;
    //
    $("#file").change(function () {
        let files = this.files;
        console.log(files[0]);
        let fd = new FormData();
        fd.append("file", files[0])
        //发送ajax
        $.ajax({
            url: "http://192.168.30.63:3000/stus/upload",
            type: "post",
            data: fd,
            processData: false,
            contentType: false,
            success(res) {
                filename = res.files.filename;
                $("#img").attr("src", 'http://192.168.30.63:3000/temp/' + filename);
            }
        })
    })

    let sid = location.search.split("=")[1];
    console.log(sid);
    $.ajax({
        url: 'http://192.168.30.63:3000/stus/getStuById',
        data: { id: sid },
        dataType: "json",
        type: "get",
        beforeSend: function () {
            $("#load").show();
        }, headers: {
            "Authorization": localStorage.token
        },
        success: function (data) {
            console.log(data);
            if (data.code == 200) {
                let head;
                if (data.data[0].head == "" || data.data[0].head == undefined) {
                    head = "http://192.168.30.63:3000/images/head1.jpg";
                } else {
                    head = "http://192.168.30.63:3000/images/" + data.data[0].head;
                }
                $("#sid").val(data.data[0]._id);
                $("#img").attr("src", head);
                $("#name").val(data.data[0].name);
                $("#age").val(data.data[0].age);
                $("#birth").val(data.data[0].birth);
                $("input[name=like]").val(data.data[0].like);
                $("input[name=gender]").val([data.data[0].gender]);
                getClass(data.data[0].clas_id);
            }
        },
        complete: function () {
            $("#load").hide();
        }, error(data) {
            if (data.status == 401) {
                alert("请登录");
                top.location.href = "/login.html"
            }
        }
    })
    //修改
    $(".xg").click(function () {
        let likes = [];
        $("input[name=like]:checked").each(function (index, item) {
            likes.push($(item).val());
        })
        console.log(JSON.stringify(likes));
        $.ajax({
            url: 'http://192.168.30.63:3000/stus/upd',
            data: { _id: $("#sid").val(), name: $("#name").val(), age: $("#age").val(), like: JSON.stringify(likes), gender: $("input[name=gender]:checked").val(), birth: $("input[name=birth]").val(), clas_id: $(".classlist").val(), filename },
            dataType: "json",
            type: "post",
            success: function (data) {
                if (data.code == 200) {
                    alert("修改成功")
                } else {
                    alert("修改失败");
                }
            }
        })
    })
    //得到班级
    function getClass(cid) {
        $.ajax({
            url: 'http://192.168.30.63:3000/clas/getClass',
            dataType: "json",
            type: "get",
            // beforeSend: function () {
            //     $("#load").show();
            // },
            success: function (data) {
                if (data.code == 200) {
                    console.log(data.result);
                    data.result.forEach(function (item, index) {
                        $(".classlist").append(`<option value="${item._id}" >${item.cname}</option>`)
                    })
                    $(".classlist").val(cid);
                }
            }
        })

    }
});
